package org.diting.collections;

import java.util.Iterator;
import java.util.NoSuchElementException;



class ArrayIterable<T> implements Iterable<T>, ICountable
{
	public ArrayIterable(T[] source)
	{
		this._source = source;
	}

	@Override
	public Iterator<T> iterator() {

		return new ArrayIterator(this._source);
	}

	private T[] _source;

	public T[] getSource()
	{
		return _source;
	}

	@Override
	public int count() {

		return _source.length;
	}
	
	private class ArrayIterator implements Iterator<T>
	{
		public ArrayIterator(T[] source)
		{
			this._source = source;
		}

		private int _index = 0;

		private T[] _source;

		@Override
		public boolean hasNext() {
			return _index < this._source.length;
		}

		@Override
		public T next() {
			if(_index < this._source.length)
			{
				T rs = this._source[this._index];
				this._index ++;
				return rs;
			}
			else
			{
				throw new NoSuchElementException();
			}
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();

		}

	}

}